#include "DatabaseFillUpDialog.h"

DatabaseFillUpDialog::DatabaseFillUpDialog( DBMS *dbms, /*DataBase *db,*/ QWidget *parent ) : QDialog( parent )
{
    //if ( db == 0 ) { Output( warning, "db = null"); return; }
    //this->db = db;
    this->db = 0;
    this->dbms =dbms;

    setWindowTitle( tr("Filling up database") );

    //!!!! CHANGE MATERIALS TEMP DATABASE !!!!
    dbms->ConnectDatabase( "Materials" );
    Table = new StockTableView( dbms->GetDatabase( "Materials" ) , this );
    //Table = 0;

    NumberDialog = 0;

    AddButton = new QPushButton( tr("Add") );
    RemoveButton = new QPushButton( tr("Remove") );

    //SaveButton = new QPushButton( tr("Save") );
    //CancelButton = new QPushButton( tr("Cancel") );
    CloseButton = new QPushButton( tr("Close") );

    //Manage layouts
    AddRemoveButtonsLayout = new QHBoxLayout();
    AddRemoveButtonsLayout->addWidget( AddButton );
    AddRemoveButtonsLayout->addWidget( RemoveButton );
    AddRemoveButtonsLayout->addStretch();

    CloseButtonLayout = new QHBoxLayout();
    CloseButtonLayout->addStretch();
    CloseButtonLayout->addWidget( CloseButton );

    MainLayout = new QVBoxLayout();
    MainLayout->addLayout( AddRemoveButtonsLayout );
    MainLayout->addWidget( Table );
    MainLayout->addLayout( CloseButtonLayout );

    this->setLayout( MainLayout );

    //SiS
    //connect( CancelButton, SIGNAL(clicked()), this, SLOT(clear()) );
    //connect( CancelButton, SIGNAL(clicked()), this, SIGNAL(RejectDatabase()) );
    //connect( SaveButton, SIGNAL(clicked()), this, SLOT(SavePressed()) );
    connect( AddButton, SIGNAL(clicked()), this, SLOT(AddPressed()) );
    connect( RemoveButton, SIGNAL(clicked()), this, SLOT(RemovePressed()) );
    connect( CloseButton, SIGNAL(clicked()), this, SLOT(ClosePressed()) );
    connect( CloseButton, SIGNAL(clicked()), this, SLOT(close()) );

}

DatabaseFillUpDialog::~DatabaseFillUpDialog()
{
    delete Table;

    delete AddButton;
    delete RemoveButton;
    delete CloseButton;

    //delete AddRemoveButtonsLayout;
    //delete CloseButtonLayout;
    //delete MainLayout;
}

void DatabaseFillUpDialog::clear()
{
    changes.clear();
    this->close();
}

void DatabaseFillUpDialog::SetDatabase( DataBase *db)
{
    this->db = db;
    Table->SetDatabase( db );
    //if ( Table != 0 ) delete Table;
    //Table = new StockTableView( this->db, this );
}

void DatabaseFillUpDialog::AddPressed()
{
    if ( NumberDialog == 0 ) {
        NumberDialog = new ItemNumberDialog( this->dbms, this );
        connect( NumberDialog, SIGNAL(Item(ItemType,int)), this, SLOT(Item(ItemType,int)) );
    }

    NumberDialog->show();
}

void DatabaseFillUpDialog::RemovePressed()
{
    StockItem item;

    switch ( Table->GetSelectedItem( item ) ){
        case 1:
                //means no row is selected
                Output( warning, tr("No item is selected") );
                break;
        case 0:
                //in item we have real stock item from database
                //no need to check for result of RemoveItem()
                if ( Output( ask, tr("Do you really want to delete this item?") ) == QMessageBox::Cancel) break;

                if ( changes.contains( item.GetArticle() ) ) changes.remove( item.GetArticle() );
                    else changes[ item.GetArticle() ] -= item.GetNumber();

                db->RemoveItem( item );
                //Output( message, tr("Item was succesfully removed") );
                break;
    }
}

/*void DatabaseFillUpDialog::SavePressed()
{
    //!!!!!!!!!!!!!!Save database to file]
    StockItem item;
    QMap<QString, int> changes;

    for ( int i=0; i < db->GetItemsCount(); i++ ){
        db->GetItem( i, item );
        changes[ item.GetArticle() ] = item.GetNumber();
    }

    //Output( message, "SavePressed is executed" );
    emit AcceptDatabase( changes );
}*/

void DatabaseFillUpDialog::ClosePressed()
{
    this->close();
}

void DatabaseFillUpDialog::Item( const ItemType& item, int count )
{
    //Output( message, "executing DatabaseFillUpDialog::Item(...)" );
    //StockItem stockItem(item);
    //Output( message, "setting number" );
    //stockItem.SetNumber( count );
    ItemType t = item;
    t.Set( 2, QString::number( count ) );

    //Output( message, "Inserting item" );

    switch ( db->InsertItem( t ) ){
        case dup_error:
            Output( message, tr("Item already exists in the order") );
            break;
        case ok:
            //item is a stock item
            changes[ item.Get( 0 ) ] += count;
            break;
    }
}

void DatabaseFillUpDialog::closeEvent( QCloseEvent * event )
{
    emit ChangesMade( changes );
    clear();
}
